﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data.SQLite;

namespace Com.Toook.Common
{
    /// <summary>
    /// PageColoumn ShowPageParam表中的字段名
    /// </summary>
    public class PageColoumn
    {
        public static string id = "id";
        public static string typeId = "typeId";
        public static string typeCode = "typeCode";
        public static string typeName = "typeName";
        public static string totalCount = "totalCount";
        public static string title = "title";
        public static string keyword = "keyword";
        public static string description = "description";
    }
    
    /// <summary>
    ///PageData 的摘要说明
    /// </summary>
    public class PageData
    {
        //PageColoumn字段顺序必须和PageData中的sql查询列顺序相同
        private static string sql = " select id, typeId, typeCode, typeName, totalCount, title, keyword, description from ShowPageParam ";
        /// <summary>
        /// 保存数据库
        /// </summary>
        private static DataTable typeParamTable = new DataTable();
        public PageData()
        {
        }
        /// <summary>
        /// 根据condition(如"typeId=2", "typeName='movie2' ")获取匹配行的数据,没有返回null
        /// </summary>
        /// <param name="condition"></param>
        /// <returns>没有返回null</returns>
        public static DataRow SelectParamRow(string condition)
        {
            if (typeParamTable.Rows.Count == 0)
            {
                InitPageData();
            }

            DataRow[] rows = typeParamTable.Select(condition);

            if (rows.Length > 0)
            {
                return rows[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 根据byCondition查询出行数据，再获取coloumnName指定列的值
        /// </summary>
        /// <param name="byCondition">条件(如"typeId=2", "typeName='movie2' ")</param>
        /// <param name="coloumnName">要获取值的列名枚举PageColoumn</param>
        /// <returns>没有返回null</returns>
        public static object GetValue(string byCondition, string coloumnName)
        {
            DataRow row = SelectParamRow(byCondition);
            if (row != null)
            {
                return row[coloumnName];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 计算分页开始值 limit start, pageSize
        /// </summary>
        /// <param name="page">从url获取的page参数</param>
        /// <param name="total">记录总数</param>
        /// <param name="pageSize">没有显示数</param>
        /// <returns></returns>
        public static int CalculateStart(string page, int total, int pageSize)
        {
            int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//总页数
            int p;
            //页数不为空且是数字，设置页码。否则第一页。
            if(!string.IsNullOrEmpty(page))
            {
                int.TryParse(page, out p);
            }
            else
            {
                p = 1;
            }
            //分页参数不合法
            if(p < 1 || p > pageCount)
            {
                p = 1;
            }

            int start = (p - 1) * pageSize;

            return start;
        }
        /// <summary>
        /// 查询数据库，初始化类型分页数据表typeParamTable
        /// </summary>
        public static void InitPageData()
        {            
            SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
            SQLiteDataAdapter da = new SQLiteDataAdapter(sql, con);
            da.Fill(typeParamTable);
        }
    }
}